package screens;

import java.util.Vector;

import log.Logger;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import runnables.AddCommentInGroupRunnable;
import runnables.GroupRunnable;
import strings.WebcampusResource;
import util.LookAndFeel;
import beans.Course;
import beans.Message;
import customfields.ButtonUADE;
import customfields.MessageField;
import customfields.WhiteLabelField;

public class GroupMessagesScreen extends TransitionScreen implements WebcampusResource{

	// RESOURCES
	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private Vector messages;
	private Course course;

	private BasicEditField messageField;

	public GroupMessagesScreen(Course cour, int transitionType){
		super(transitionType);
		
		Logger.log("creating group messages screen");

		this.course=cour;
		this.messages=cour.getGroup().getMessages();

			
		// Main vertical field manager
		VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_HEIGHT | VERTICAL_SCROLLBAR | VERTICAL_SCROLL);
		Bitmap background_bitmap = Bitmap.getBitmapResource(LookAndFeel.background_img);
		Bitmap background_bitmap_resized = new Bitmap(Display.getWidth(), background_bitmap.getHeight());
		background_bitmap.scaleInto(background_bitmap_resized, Bitmap.FILTER_LANCZOS | Bitmap.SCALE_TO_FILL);
		Background background = BackgroundFactory.createBitmapBackground(background_bitmap_resized);
		vfm.setBackground(background);

		// Header
		Bitmap header_img = Bitmap.getBitmapResource(LookAndFeel.header_img);
		Bitmap header_img_resized = new Bitmap(Display.getWidth(), (int) (Display.getHeight()*0.25));
		header_img.scaleInto(header_img_resized, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
		BitmapField header = new BitmapField(header_img_resized,Field.FIELD_HCENTER);
		vfm.add(header);

		// Separator
		vfm.add(new SeparatorField());
		vfm.add(new NullField(Field.FOCUSABLE));

		WhiteLabelField messageTitle = new WhiteLabelField(resources.getString(MESSAGE_TITLE));
		vfm.add(messageTitle);
		messageField = new BasicEditField("", "", 250, BasicEditField.FIELD_LEFT);
		messageField.setBackground(BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color,
				LookAndFeel.button_upper_color, LookAndFeel.button_lower_color, LookAndFeel.button_lower_color));
		vfm.add(messageField);
		ButtonUADE send = new ButtonUADE(resources.getString(SEND_BUTTON),Field.FIELD_HCENTER );
		send.setChangeListener(sendListener);
		vfm.add(new LabelField(""));
		vfm.add(send);
		vfm.add(new LabelField(""));

	
		if (!messages.isEmpty() && messages!=null){
			vfm.add(new MessageField((Message) messages.elementAt(0)));
		}
		for (int i=1; i<messages.size(); i++){
			vfm.add(new NullField(Field.FOCUSABLE));
			vfm.add(new SeparatorField());
			vfm.add(new MessageField((Message) messages.elementAt(i)));
		}
		vfm.add(new SeparatorField());
		vfm.add(new LabelField(""));
		add(vfm);
	}

	private FieldChangeListener sendListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			DownloadingPopupScreen.showScreenAndWait(new AddCommentInGroupRunnable(course, messageField.getText()),
					resources.getString(PUPUP_SENDING));
		}
	};

	// dialogs
	public static void showError(String message){
		Dialog.alert(message);
	}

	protected boolean onSavePrompt() {
		return true;
	}
	
	// Menu Item for 'Refresh'
	static final class RefreshMenuItem extends MenuItem
	{
		Course course;
		public RefreshMenuItem(Course c, String text, int ordinal, int priority){
			super(text, ordinal, priority);
			this.course=c;
		}
		public void run() {
			PleaseWaitPopupScreen.showScreenAndWait(new GroupRunnable(this.course, Controller.getInstance().getHash(),true), resources.getString(POPUP_REFRESHING));
		}
	} 
}
